<?php


namespace app\controller;


use app\BaseController;

use app\entiity\R;
use app\service\ArticleService;

class ArticleController extends BaseController
{
    private $articleService;

    /**
     * ArticleController constructor.
     * @param $articleService
     */
    public function __construct()
    {
        $this->articleService = ArticleService::getIncetance();
    }

    public function getArticle($id){
        $result = $this->articleService->getArticle($id);
        return R::ok_Data($result);
    }

    public function getArticleList(){
        $data = [
            'type'=>input('param.type'),
            'page'=>input('param.page'),
            'limit'=>input('param.limit'),
            'status'=>input('param.status')
        ];
        $result = $this->articleService->getArticleList($data);
        return R::ok_Data($result);
    }

    public function addArticle(){
        $data = [
            'title'=>input('param.title'),
            'content'=>input('param.content'),
            'type'=>input('param.type'),
            'status'=>input('param.status'),
            'create_time'=>date('Y-m-d h:i:s', time()),
            'update_time'=>date('Y-m-d h:i:s', time())
        ];
        try {
            $this->articleService->addArticle($data);
            return R::ok();
        }catch (\Exception $e){
            return R::err();
        }
    }

    public function deleteArticle($id){
        try {
            $this->articleService->deleteById($id);
            return R::ok();
        }catch (\Exception $e){
            return R::err();
        }
    }

    public function updateArticle(){
        $data = [
            'id'=>input('param.id'),
            'title'=>input('param.title'),
            'content'=>input('param.content'),
            'type'=>input('param.type'),
            'status'=>input('param.status')
        ];
        try {
            $this->articleService->updateArticle($data);
            return R::ok();
        }catch (\Exception $e){
            echo $e;
            return R::err();
        }
    }
}